<template>
  <div class="min-h-screen flex items-center justify-center bg-gradient-to-br from-blue-50 to-indigo-50 p-4">
    <el-card class="w-full max-w-md shadow-xl overflow-hidden">
      <div class="bg-blue-600 text-white p-6 text-center">
        <el-icon class="w-12 h-12 mx-auto mb-2"><Document /></el-icon>
        <h2 class="text-2xl font-bold">欢迎登录课程学习平台</h2>
        <p class="text-blue-100 mt-1">请输入您的账号信息</p>
      </div>
      
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="p-6 space-y-4"
        @submit.prevent="handleLogin"
      >
        <el-form-item label="用户名/学号" prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入学号"
            :prefix-icon="UserFilled"
            clearable
          />
        </el-form-item>
        
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            show-password
          />
        </el-form-item>

        <el-form-item>
          <div class="flex items-center justify-between">
            <el-checkbox v-model="loginForm.remember" label="记住我" />
            <el-link type="primary" href="#">忘记密码?</el-link>
          </div>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            size="large"
            class="w-full"
            :loading="loading"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>

      <div class="bg-gray-50 p-4 text-center">
        <el-link type="default" :underline="false">
          还没有账号? 
          <router-link to="/register" class="text-blue-600 font-medium">立即注册</router-link>
        </el-link>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '../store/auth'
import { Document, UserFilled, Lock } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const router = useRouter()
const auth = useAuthStore()
const loginFormRef = ref()
const loading = ref(false)

// 设置默认用户名和密码，方便访客直接登录
const loginForm = reactive({
  username: '23002943',
  password: '123456',
  remember: false
})

// 表单验证规则
const loginRules = {
  username: [
    { required: true, message: '请输入用户名/学号', trigger: 'blur' },
    { min: 4, max: 20, message: '用户名长度在 4 到 20 个字符之间', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符之间', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  try {
    await loginFormRef.value.validate()
    loading.value = true

    // 调用登录方法，现在会抛出错误而不是返回false
    await auth.login(loginForm.username, loginForm.password)

    // 只有登录成功才会执行到这里
    ElMessage.success('登录成功')
    router.push('/courses')

  } catch (error) {
    // 处理登录错误
    if (error.name === 'validate') {
      // 表单验证错误，不需要处理
      return
    }

    // 显示具体的错误信息
    ElMessage.error(error.message || '登录失败，请检查用户名和密码')
  } finally {
    loading.value = false
  }
}
</script>